import 'package:flustars/flustars.dart';
import 'package:zrjz/comm/constants/app_constant.dart';
import 'package:zrjz/comm/http/http_provider.dart';
import 'package:zrjz/comm/http/http_result.dart';
import 'package:zrjz/comm/constants/api_constants.dart';
import 'package:get/get.dart';
import 'package:zrjz/comm/utils/util.dart';
import 'package:zrjz/entity/response/user_login.dart';
import 'package:zrjz/entity/response/user_profile.dart';
import 'package:zrjz/entity/response/verify_code.dart';

/// 用户相关
class AuthRepository extends GetxService {
  HttpProvider httpProvider;

  AuthRepository({required this.httpProvider});

  /// 获取发送验证码的图片，用于发送短信
  Future<HttpResult<VerifyCode>> getVerifyCodeImage() async {
    //Get请求
    final result = await httpProvider.requestNetResult(ApiConstants.apiVerifyCodeImage);

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = VerifyCode.fromJson(json!);
      //重新赋值data或list
      return result.convert<VerifyCode>(data: data);
    }
    return result.convert<VerifyCode>();
  }


  /// 获取用户详情数据
  Future<HttpResult<UserProfile>> fetchUserProfile() async {
    //POST请求
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiUserProfile,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = UserProfile.fromJson(json!);
      //重新赋值data或list
      return result.convert<UserProfile>(data: data);
    }
    return result.convert();
  }
}
